<%@page import="net.groupme.search.GroupMeSearchEngine"%>
<%@page import="net.groupme.user.UserManager"%>
<%@page import="java.util.List"%>
<%@page import="net.groupme.model.Resource"%>
<%@page import="net.groupme.model.*"%>
<%@page import="java.util.*"%>
<%@page import="org.springframework.web.context.support.WebApplicationContextUtils"%>
<%@page import="net.groupme.gui.constants.VIEW_VOCABULARY"%>
<%@page import="net.groupme.model.ObjectFactory"%>
<%@page import="net.groupme.model.MODEL_CONSTANTS"%>
<%@page import="net.groupme.gui.rendering.TagRenderer"%>
<div class="grouplistHead" style="margin-top: 1em; margin-bottom: 1em;">
	<div class="groupListHeadMenu">
	</div>
	<% String searchString = "" + request.getAttribute(VIEW_VOCABULARY.PARAMETER_KEYWORD);
	if(searchString == null){
		searchString = request.getParameter(VIEW_VOCABULARY.PARAMETER_KEYWORD);
	}
	GroupMeSearchEngine se = (GroupMeSearchEngine) WebApplicationContextUtils.getWebApplicationContext(pageContext.getServletContext())
		.getBean(VIEW_VOCABULARY.GROUPME_SEARCH_ENGINE, GroupMeSearchEngine .class); 
	
	UserManager um = (UserManager) WebApplicationContextUtils.getWebApplicationContext(pageContext.getServletContext())
		.getBean(VIEW_VOCABULARY.USER_MANGER, UserManager.class); 
	Tag searchTag = ObjectFactory.getTag(searchString);
	List<Resource> myresources = se.getUserSpecificSearchStrategy().searchForResourcesDirectlyTaggedOrContainedInRelatedGroupOfUser(um.getUser(), searchTag);
	int resultListSize = myresources.size();

%>

	<h4 style="margin-bottom: 3px">Your groups and resources associated with <i>"<%= searchString %>"</i>
	</h4>
	<span style="font-size: x-small; font-weight: 100">Number of groups/resources: <%= myresources.size() %> </span><br/>
	<span style="font-size: x-small; font-weight: 100">Explore other groups/resources that are related to <i><%= searchString %></i>: 
	<% 	String tmpKeywordDelimeter = "";	
		if(searchString.contains(" ")){
			tmpKeywordDelimeter = "%22";
		}else{
			tmpKeywordDelimeter = "";
		}
	%>
	<a href="<%= searchTag.getURI()  %>" title="Explore Groups and Resources related to <%= searchString %>">search</a>
	</span>
</div>


<div id="mainGroupwindowContent">

<%

//current page that is visualized
int currentPage = 0;
if(request.getParameter("pageID") != null){
	currentPage = Integer.parseInt(request.getParameter("pageID"));
}else {
	currentPage = 1;
}

//Iterator it = myresources.iterator();
int iterStart = (currentPage * 10) - 10;
int iterEnd = iterStart + 9;

//default images
String videoForeground = request.getContextPath() + "/images/video_fg.png";
String defaultVideo = request.getContextPath() + "/images/video_fg2.png";
String defaultgroupThumbnail = request.getContextPath() + "/images/groupimg-default.gif";
String groupImageError = request.getContextPath() + "/images/groupErrorImg.png";
String groupBorder = request.getContextPath() + "/images/group_bg.png";
String groupBackground = request.getContextPath() + "/images/group.png";
String brokenImage = request.getContextPath() + "/images/broken_img.png";

ListIterator<Resource> it = myresources.listIterator(iterStart);
while (it.hasNext() && iterStart <= iterEnd){ 
	iterStart = iterStart +1;
	
	String requestType = VIEW_VOCABULARY.REQUEST_TYPE_RESOURCE;
	String thumbnail = null;
	String videoThumbnail = null;
	String videoURL = null;
	String imageAltAttribute = "";
	int imageWidth = 16;
	int imageHeight = 16;
	boolean isGroup = false;
	boolean isImage = false;
	boolean isVideo = false;
	
		Resource actualResource = (Resource) it.next();
		//resource is a group
		if (MODEL_CONSTANTS.RESOURCE_TYPE_GROUP.equals(actualResource.getType())){
			requestType = VIEW_VOCABULARY.RESTful_GROUP_URI_PATTERN;
			String groupID = actualResource.getId();
			Group actualItem = ObjectFactory.getGroup(groupID);
			isGroup = true;
			imageAltAttribute = "Group";
			imageHeight = 65;
			imageWidth = 105;
			Iterator<User> userIter = actualItem.getUsers("owner").iterator();
			User groupOwner = null;
			if (userIter.hasNext()){
				groupOwner = userIter.next();
			}
			
			try{
				thumbnail = actualItem.getActualGroupVisualization(groupOwner.getId()).getThumbnail();
			}
			catch(Exception e){
				
			}
			
			if (thumbnail== null){
				thumbnail = actualItem.getURLOfAPictureInGroup();
			}
				
			if (thumbnail== null & actualItem.getURLOfAPictureInGroup()==null){
				thumbnail = defaultgroupThumbnail;
			}
		//resource is an image
		}else if(MODEL_CONSTANTS.RESOURCE_TYPE_IMAGE.equals(actualResource.getType())){
			thumbnail = actualResource.getURL().toString();
			isImage = true;
			imageAltAttribute = "Image";
			imageHeight = 65;
			imageWidth = 105;
		
		}//resource is an audio file
		else if(MODEL_CONSTANTS.RESOURCE_TYPE_AUDIO.equals(actualResource.getType())){
			thumbnail = request.getContextPath() + "/images/audio.png";
			imageAltAttribute = "Audio";
		}//resource is a video file
		else if(MODEL_CONSTANTS.RESOURCE_TYPE_VIDEO.equals(actualResource.getType())){
			thumbnail = defaultVideo;
			imageHeight = 65;
			imageWidth = 105;
			imageAltAttribute = "Video";
		}//resource is a shockwave file
		else if(MODEL_CONSTANTS.RESOURCE_TYPE_SHOCKWAVE.equals(actualResource.getType())){
			
			videoURL = actualResource.getURL().toString();
			//youtube video?
			if(videoURL.contains("youtube")){
				isVideo = true;
				thumbnail = "http://img.youtube.com/vi/" + videoURL.substring(videoURL.lastIndexOf("/") + 1) + "/2.jpg";
				imageHeight = 65;
				imageWidth = 105;
				
			}else {
				thumbnail = request.getContextPath() + "/images/flash16x16.png";
			}
			imageAltAttribute = "Shockwave";
		
		} //resource is a website
		else if(MODEL_CONSTANTS.RESOURCE_TYPE_WEBSITE.equals(actualResource.getType())){
			thumbnail = request.getContextPath() + "/images/page_world.png";
			imageAltAttribute = "Website";
		}//resource is a rss-feed
		else{
			thumbnail = request.getContextPath() + "/images/rss.png";
			imageAltAttribute = "RSS Feed";
		}
	
		%>
		
						<div class="resultListItem" <% if(isGroup){out.print("style=\"min-height:80px; background-image:url(" + groupBackground + "); background-repeat:repeat-x;\"");} %>>
						  	<div style="float:left; margin-right:1em; <% if (isGroup){out.print("background-image:url(" + groupBorder + "); background-repeat:no-repeat; height:80px; width:120px;");} else if(isVideo){out.print("background-image:url(" + thumbnail + "); background-repeat:no-repeat; height:65px; width:105px;");} %>">
					   			<a href="<%= requestType + "/" + actualResource.getId()%>">
					   				<img src="<%if(isVideo){out.print(videoForeground); } else {out.print(thumbnail);}%>" width="<%=imageWidth %>" height="<%=imageHeight %>" alt="<%=imageAltAttribute %>" title="<%=actualResource.getType() %>" <%if(isGroup) {%> onerror="this.src='<%=groupImageError %>';"<%} else if (isImage){%> onerror="this.src='<%=brokenImage %>';"<%}%>/>
					   			</a>
					   		</div>
								<strong>
									<a href="<%= actualResource.getURI()%>">
										<% if(actualResource.getName().length() == 0 || actualResource.getName() == null){
											out.print("no name (" + actualResource.getType() + ")");
										}else {
											out.print(actualResource.getName());
										}
										%>
									</a>
								</strong>
								<p> 
								<span style="font-size:0.8em; color:black">
								<% 
								
								if(actualResource.getDescription() != null){
									
									if(actualResource.getDescription().length() != 0){
										
											out.print(actualResource.getDescription());
											
									}
										
									else out.print("No description");
									
								}else {
									out.print("No description");
								}
								%>
								</span>
								</p>
						<%
			
			
						Collection<Tag> resourceTags = actualResource.getTags();
						Iterator<Tag> tagIterator = resourceTags.iterator();
						
					    if(tagIterator.hasNext()) {
							%>
							<small><strong>Tags:</strong>
							<%
						   
						   while(tagIterator.hasNext()){
							   Tag currentTag = tagIterator.next();%>
							   <a href="<%= TagRenderer.getTagURLForPersonalTagCloud(currentTag) %>" title="Search for groups and resources related with <%=currentTag.getKeyword() %>"><%=deleteRedundantCommas(currentTag.getKeyword()) %></a>
							   <% if(tagIterator.hasNext()){ out.print(" - ");} %>
								   <%
						   }
							%>
							</small>
							<%
					    }else { 
					    	%>
					    	<small><strong>Tags:</strong> <i>no tags</i></small>
					    	<%}%>
						   	</div>
						 
						 <br style="clear:left"/>
		
		
		<%
}

%>

</div>
<div id="pagenumbers" style="text-align: center; font-size: 0.8em; margin-top: 2em;">
	<%
		
		
		
		//int resultListSize = 200;
		if(resultListSize > 10){
			int numberofResultpages = (int) Math.ceil(resultListSize / 10.0);
			if(currentPage > 1){
				int showPrev = currentPage-1;
				%>
				<span><strong><a href="<%= VIEW_VOCABULARY.RESTful_MyGroupMe_TAG_SEARCH + searchString + "?pageID=" + showPrev %>">previous</a></strong></span>&nbsp;&nbsp;
				<%
			}
			out.print("<span>");
			if(numberofResultpages <= 10){
				for(int i = 1; i <= numberofResultpages; i++){
					%>
					<a href="<%= VIEW_VOCABULARY.RESTful_MyGroupMe_TAG_SEARCH + searchString + "?pageID=" + i %>" <%if(i == currentPage){out.print("style=\"font-weight:bold;color:red;\"");}%>><%=i %></a>
					<%
				}
			}else {
				if(numberofResultpages - currentPage >= 10){
					int start = 0;
					int end = 0;
					
					if(currentPage < 7){
						start = 1;
						end = 7;
					}else {
						
						%>
						<a href="<%= VIEW_VOCABULARY.RESTful_MyGroupMe_TAG_SEARCH + searchString + "?pageID=" + 1 %>"><%=1 %></a>
						<%
						out.print("&nbsp;...&nbsp;");
						start = currentPage - 3;
						end = currentPage + 3;
					}
					
					for(int i = start; i <= end; i++){
						%>
						<a href="<%= VIEW_VOCABULARY.RESTful_MyGroupMe_TAG_SEARCH + searchString + "?pageID=" + i %>" <%if(i == currentPage){out.print("style=\"font-weight:bold;color:red;\"");}%>><%=i %></a>
						<%
					}
					out.print("&nbsp;...&nbsp;");
					%>
					<a href="<%= VIEW_VOCABULARY.RESTful_MyGroupMe_TAG_SEARCH + searchString + "?pageID=" + numberofResultpages %>"><%=numberofResultpages %></a>
					<%
				}
				else {
					%>
					<a href="<%= VIEW_VOCABULARY.RESTful_MyGroupMe_TAG_SEARCH + searchString + "?pageID=" + 1 %>"><%=1 %></a>
					<%
					out.print("&nbsp;...&nbsp;");
					//out.print("...&nbsp;");
					for(int i = numberofResultpages - 10; i <= numberofResultpages; i++){
						%>
						<a href="<%= VIEW_VOCABULARY.RESTful_MyGroupMe_TAG_SEARCH + searchString + "?pageID=" + i %>" <%if(i == currentPage){out.print("style=\"font-weight:bold;color:red;\"");}%>><%=i %></a>
						<%
					}
				}
			}
			out.print("</span>");
			if(currentPage < numberofResultpages){
				int showNext = currentPage+1;
				%>
				<span><strong><a href="<%= VIEW_VOCABULARY.RESTful_MyGroupMe_TAG_SEARCH + searchString + "?pageID=" + showNext %>">next</a></strong></span>&nbsp;&nbsp;
				<%
			}
		}
	%>
</div>